package org.hbhk.hxl.gateway.boot;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtil implements ApplicationContextAware {
	private static ApplicationContext context;

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}

	public static ApplicationContext getApplicationContext() {
//		// 所有的bean,参考：http://412887952-qq-com.iteye.com/blog/2314051
//		String[] beanNames = context.getBeanDefinitionNames();
//		// String[] beanNames =
//		// ctx.getBeanNamesForAnnotation(RestController.class);//所有添加该注解的bean
//		int i = 0;
//		for (String str : beanNames) {
//			System.out.println(str+",beanName:{}"+context.getBean(str).getClass().getName());
//		}

		return context;
	}

	public static void unregisterBean(String beanId) {
		ConfigurableApplicationContext configurableContext = (ConfigurableApplicationContext) context;
		DefaultListableBeanFactory beanDefinitionRegistry = (DefaultListableBeanFactory) configurableContext
				.getBeanFactory();
		System.out.println(beanDefinitionRegistry.containsBean(beanId));
		//beanDefinitionRegistry.removeBeanDefinition(beanId);
	}

}
